package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.ChannelPageDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/3/16 10:51
 * @Version 1.0
 */
@Service
@Slf4j
@Transactional
@RequiredArgsConstructor
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    private final WmChannelMapper wmChannelMapper;
    private final WmNewsMapper wmNewsMapper;
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }


    @Override
    public ResponseResult queryPageChannel(ChannelPageDto dto) {
        dto.jiancha();
        IPage p= wmChannelMapper.selectPage(new Page<>(dto.getPage(),dto.getSize()),new LambdaQueryWrapper<WmChannel>()
                .like(dto.getName()!=null||dto.getName()!="",WmChannel::getName,dto.getName()));
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)p.getTotal());
        result.setData(p.getRecords());
        return result;
    }
    @Override
    public ResponseResult addChannel(ChannelDto dto) {
        if(dto.getName()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel wmChannel = wmChannelMapper.selectOne(new LambdaQueryWrapper<WmChannel>()
                .eq(WmChannel::getName, dto.getName()));
        if(wmChannel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmChannel channel=new WmChannel();
        channel.setName(dto.getName());
        if(dto.getDescription()!=null){
            channel.setDescription(dto.getDescription());
        }
        if(dto.getCreatedTime()!=null){
            channel.setCreatedTime(dto.getCreatedTime());
        }else {
            channel.setCreatedTime(new Date());
        }
        if(dto.getAbc()!=null){
            if(dto.getAbc()==true){
                channel.setIsDefault(WemediaConstants.CHANNEL_DEFAULT_YI);
            }channel.setIsDefault(WemediaConstants.CHANNEL_DEFAULT_LING);
        }else {
            channel.setIsDefault(WemediaConstants.CHANNEL_DEFAULT_YI);
        }
        if(dto.getOrd()==null){
            channel.setOrd(WemediaConstants.CHANNEL_ORD_LING);
        }else {
            channel.setOrd(dto.getOrd());
        }
        if(dto.getStatus()!=null){
            if(dto.getStatus()==true){
                channel.setStatus(WemediaConstants.CHANNEL_STATUS_YI);
            }channel.setStatus(WemediaConstants.CHANNEL_STATUS_LING);
        }else {
            channel.setStatus(WemediaConstants.CHANNEL_STATUS_YI);
        }
        wmChannelMapper.insert(channel);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult modifyChannel(ChannelDto dto) {
        if (dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(dto.getId());
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        List<WmNews> wmNews = wmNewsMapper.selectList(new LambdaQueryWrapper<WmNews>()
                .eq(WmNews::getChannelId, dto.getId()));
        if(dto.getStatus()!=null){
        if(!wmNews.isEmpty()&&dto.getStatus()==false){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }}
        if(dto.getName()!=null &&dto.getName()!=""){
            wmChannel.setName(dto.getName());
        }
        if(dto.getDescription()!=null &&dto.getDescription()!=""){
            wmChannel.setDescription(dto.getDescription());
        }
        if(dto.getAbc()!=null){
            wmChannel.setIsDefault(dto.getAbc()==true?WemediaConstants.CHANNEL_DEFAULT_YI:WemediaConstants.CHANNEL_DEFAULT_LING);
        }
        if(dto.getStatus()!=null){
            wmChannel.setStatus(dto.getStatus()==true?WemediaConstants.CHANNEL_STATUS_YI:WemediaConstants.CHANNEL_STATUS_LING);
        }
        if(dto.getOrd()!=null){
            wmChannel.setOrd(dto.getOrd());
        }
        wmChannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult deleteChannel(String id) {
        if(StringUtils.isEmpty(id)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if(wmChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if(wmChannel.getStatus()==WemediaConstants.CHANNEL_STATUS_YI){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }else {
            wmChannelMapper.deleteById(id);
            return ResponseResult.okResult(null);
        }
    }
}